﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HtmlAgilityPack;
using NPOI.HSSF.UserModel;
using Test;
using HtmlDocument = HtmlAgilityPack.HtmlDocument;
using System.IO;
using System.Threading;
using System.Net;

namespace WebForm
{
    public partial class yupoo : Form
    {
        private List<Infors> m_infos;
        private object m_locker = new object();
        int countSubCateLoaded = 0;
        int countProLoaded = 0;
        Boolean flag, isPause;
        public yupoo()
        {
            InitializeComponent();

            webBrowser1.ScriptErrorsSuppressed = true;
            m_infos = new List<Infors>();
            textBox1.Text = "http://aiaing.v.yupoo.com";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_infos.Clear();

            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("Please input category address & output directory", "Warning!", MessageBoxButtons.OK);
                this.button1.Enabled = true;
                return;
            }
            if (!Directory.Exists(textBox2.Text))
            {
                MessageBox.Show("Ouput path is not valid!", "Warning!", MessageBoxButtons.OK);
                this.button1.Enabled = true;
                return;
            }
            // hlisting offer-sale 
            this.button1.Enabled = false;
            this.btnStop.Enabled = true;
            this.btnPause.Enabled = true;




            flag = false;
            //countProLoaded = 0;
            //countSubCateLoaded = 0;

            //int totalSubCategory = CountTotalSubNormal(textBox1.Text.Trim()) + CountSubCategoryPage();

            //MessageBox.Show("             Have  " + totalSubCategory + " subcategory \n  Please wating to load category");
          
            
            //Thread secondThread;
            //ThreadStart starter = () => LoadNormal();
            //secondThread = new Thread(starter);
            //secondThread.Start();



            //Thread thirdThread;
            //ThreadStart starter2 = () => LoadPageNumber();
            //thirdThread = new Thread(starter2);
            //thirdThread.Start();


            // LoadNormal();

            var lstCategory = new List<Category>();
            lstCategory = LoadCategorys(textBox1.Text.Trim());
            MessageBox.Show("Please Wait Load This Page");
            if (backgroundWorker1.IsBusy)
            {
                backgroundWorker1.CancelAsync();
            }
            else
            {
                //progressBar1.Value = progressBar1.Minimum;
                //btnSearch.Text = "Stop";
                //listView1.Items.Clear();
                backgroundWorker1.RunWorkerAsync();
            }

            //List<string> list = LoadNumberPage();
            //if (list.Count > 0)
            //{
            //    for (int i = 0; i < list.Count; i++)
            //    {
            //        //Thread secondThread;
            //        //ThreadStart starter = () => LoadNormal(list[i]);
            //        //secondThread = new Thread(starter);
            //        //secondThread.Start();

            //        if (backgroundWorker1.IsBusy)
            //        {
            //            backgroundWorker1.CancelAsync();
            //        }
            //        else
            //        {
            //            //progressBar1.Value = progressBar1.Minimum;
            //            //btnSearch.Text = "Stop";
            //            //listView1.Items.Clear();
            //            backgroundWorker1.RunWorkerAsync(list[i]);
            //        }
            //    }
            //}

            if (backgroundWorker2.IsBusy)
            {
                backgroundWorker2.CancelAsync();
            }
            else
            {
                //progressBar1.Value = progressBar1.Minimum;
                //btnSearch.Text = "Stop";
                //listView1.Items.Clear();
                backgroundWorker2.RunWorkerAsync();
            }


        }
        int totalSubCategory = 0;
        private int CountTotalSubNormal(string s)
        {
            var lstCategory = new List<Category>();
            lstCategory = LoadCategorys(s);



            foreach (var category in lstCategory)
            {


                totalSubCategory += category.SubCategorys.Count;



            }

            //int count = CountTatalSubCategory_Product()[0];
            //MessageBox.Show("             Have " + totalSubCategory + " subcategory \n  Please wating to load category");
            return totalSubCategory;
        }
        private void LoadFirstPage()
        {
            List<Category> listCategory = new List<Category>();
            listCategory = LoadCategorysThread(textBox1.Text.Trim());
            BuildTree(listCategory, textBox1.Text.Trim());
            //Thread thread= new Thread()




        }
        private void LoadPageNumber()
        {
            List<string> list = LoadNumberPage(textBox1.Text.Trim());
            for (int i = 0; i < list.Count; i++)
            {
                List<Category> lstCategory = new List<Category>();
                lstCategory = LoadCategorysThread(list[i]);
                BuildTree(lstCategory, list[i]);

            }
        }
        private int CountSubCategoryPage()
        {
            int s = 0;
            List<string> list = LoadNumberPage(textBox1.Text.Trim());
            for (int i = 0; i < list.Count; i++)
            {
                s = CountTotalSubNormal(list[i]);
            }
            return s;

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

            while (true)
            {
                if ((backgroundWorker1.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    LoadFirstPage();
                    //List<Category> lstCategory = new List<Category>();
                    //lstCategory = LoadCategorysThread();
                    //BuildTree(lstCategory);
                    //break;
                }
            }

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Finish!!");
        }
        // add node || 
        private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            List<Category> lstCategory = new List<Category>();
            //lstCategory = LoadCategorysThread();
            //AddNodeToNode(lstCategory);
            //List<object> arguments = e.Argument as List<object>;

            //Category category = (Category)arguments[0];
            //TreeNode treeParent = (TreeNode)arguments[1];
            //int totalSubCategory = (int)arguments[2];
            //int totalProduct = (int)arguments[3];
            //LoadProduct(category, treeParent, totalSubCategory, totalProduct);
            while (true)
            {
                if ((backgroundWorker2.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    LoadPageNumber();
                    //List<Category> lstCategory = new List<Category>();
                    //lstCategory = LoadCategorysThread();
                    //BuildTree(lstCategory);
                    //break;
                }
            }

            

        }

        private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Finish!!");

        }
        //private void WorkerDoWork(Category category, TreeNode treeParent, int totalSubCategory, int totalProduct)
        //{

        //}
        private List<string> LoadNumberPage(string sl)
        {
            var lstCategory = new List<Category>();
            var doc = new HtmlDocument();
            doc.LoadHtml(GetGeneratedHTMLThread(sl));
            List<string> list = new List<string>();
            //var names = doc.DocumentNode.SelectSingleNode("//span//font//font");
            //MessageBox.Show("Link" + names.Attributes[0].Value);
            var subCateList = doc.DocumentNode.SelectNodes("//div[@class='pager']//a");
            if (subCateList != null)
            {
                foreach (HtmlNode subCateItem in subCateList)
                {
                    string s;
                    //HtmlDocument subDoc = new HtmlDocument();
                    //subDoc.LoadHtml(subCateItem.InnerHtml);
                    //SubCategory objSubCategory = new SubCategory();
                    //objSubCategory.Link = subDoc.DocumentNode.SelectSingleNode("//a").Attributes[0].Value;
                    //objSubCategory.Name = subDoc.DocumentNode.SelectSingleNode("//h4//a[@class='Seta']").InnerHtml;
                    var value = subCateItem.InnerHtml;
                    s = subCateItem.Attributes["href"].Value;
                    int num;
                    bool res = int.TryParse(value, out num);

                    if (s.Contains("/") && res == true)
                    {
                        list.Add(textBox1.Text.Trim() + s);
                    }
                    //category.SubCategorys.Add(objSubCategory);
                    //MessageBox.Show("Value" + s);

                }

            }
            return list;
            //if (categoryNames != null)
            //{
            //    foreach (var node in categoryNames)
            //    {
            //        Category category = new Category();
            //        HtmlDocument cateDoc = new HtmlDocument();
            //        cateDoc.LoadHtml(node.InnerHtml);
            //        var names = cateDoc.DocumentNode.SelectSingleNode("//a");
            //        if (names != null)
            //        {
            //            category.Name = names.InnerHtml;
            //            category.Link = names.Attributes[0].Value;
            //            MessageBox.Show("Value" + category.Link);
            //        }
            //    }
            //}

        }
        private List<Category> LoadCategorys(string s)
        {
            var lstCategory = new List<Category>();
            var doc = new HtmlDocument();
            doc.LoadHtml(GetHmlSourceWeb(s));

            var categoryNames = doc.DocumentNode.SelectNodes("//fieldset[@class='albums clearfix']");
            if (categoryNames != null)
            {
                foreach (var node in categoryNames)
                {
                    Category category = new Category();
                    HtmlDocument cateDoc = new HtmlDocument();
                    cateDoc.LoadHtml(node.InnerHtml);
                    var names = cateDoc.DocumentNode.SelectSingleNode("//legend//a");
                    if (names != null)
                    {
                        category.Name = names.InnerHtml;
                        category.Link = names.Attributes[0].Value;
                    }
                    var subCateList = cateDoc.DocumentNode.SelectNodes("//div[@class='Sets']");
                    if (subCateList != null)
                    {
                        foreach (HtmlNode subCateItem in subCateList)
                        {
                            HtmlDocument subDoc = new HtmlDocument();
                            subDoc.LoadHtml(subCateItem.InnerHtml);
                            SubCategory objSubCategory = new SubCategory();
                            objSubCategory.Link = subDoc.DocumentNode.SelectSingleNode("//div[@class='SetCase']//a").Attributes[0].Value;
                            objSubCategory.Name = subDoc.DocumentNode.SelectSingleNode("//h4//a[@class='Seta']").InnerHtml;


                            category.SubCategorys.Add(objSubCategory);


                        }
                        lstCategory.Add(category);
                    }

                }

            }
            return lstCategory;

        }
        private List<Category> LoadCategorysThread(string s)
        {
            var lstCategory = new List<Category>();
            var doc = new HtmlDocument();
            doc.LoadHtml(GetGeneratedHTMLThread(s));

            var categoryNames = doc.DocumentNode.SelectNodes("//fieldset[@class='albums clearfix']");
            if (categoryNames != null)
            {
                foreach (var node in categoryNames)
                {
                    Category category = new Category();
                    HtmlDocument cateDoc = new HtmlDocument();
                    cateDoc.LoadHtml(node.InnerHtml);
                    var names = cateDoc.DocumentNode.SelectSingleNode("//legend//a");
                    if (names != null)
                    {
                        category.Name = names.InnerHtml;
                        category.Link = names.Attributes[0].Value;
                    }

                    var subCateList = cateDoc.DocumentNode.SelectNodes("//div[@class='Sets']");
                    if (subCateList != null)
                    {
                        foreach (HtmlNode subCateItem in subCateList)
                        {
                            HtmlDocument subDoc = new HtmlDocument();
                            subDoc.LoadHtml(subCateItem.InnerHtml);
                            SubCategory objSubCategory = new SubCategory();
                            objSubCategory.Link = subDoc.DocumentNode.SelectSingleNode("//div[@class='SetCase']//a").Attributes[0].Value;
                            objSubCategory.Name = subDoc.DocumentNode.SelectSingleNode("//h4//a[@class='Seta']").InnerHtml;


                            category.SubCategorys.Add(objSubCategory);


                        }
                        lstCategory.Add(category);
                    }

                }

            }
            return lstCategory;
        }

        private int CountTotalSubCategory(string s)
        {

            var lstCategory = new List<Category>();
            lstCategory = LoadCategorysThread(s);
            int totalSubCategory = 0;


            foreach (var category in lstCategory)
            {


                totalSubCategory += category.SubCategorys.Count;



            }
            return totalSubCategory;

        }

        //add node to tree
        private delegate void AddCallback(TreeNode node);
        private void AddNode(TreeNode nodes)
        {
            if (this.treeView1.InvokeRequired)
            {
                this.treeView1.BeginInvoke(new AddCallback(AddNode), (object)nodes);
            }
            else
            {
                this.treeView1.Nodes.Add(nodes);
            }
            //treeView1.Refresh();

        }
        //add node to node
        private delegate void AddCallback2(TreeNode node, TreeNode node2, string rootText, string s, int total, int totalSubCate, int totalPro,
       int countSubCateLoaded, int countProLoaded);
        private void AddNode2(TreeNode node, TreeNode root, string rootText, string s, int total, int totalSubCate, int totalPro,
            int countSubCateLoaded, int countProLoaded)
        {
            //root = new TreeNode();
            string sSubCateNumber = countSubCateLoaded.ToString();

            string sProductNumber = countProLoaded.ToString();

            if (this.treeView1.InvokeRequired)
            {
                this.treeView1.BeginInvoke(new AddCallback2(AddNode2), (object)node, (object)root, (object)rootText,
                    (object)s, (object)total, (object)totalSubCate
                    , (object)totalPro, (object)countSubCateLoaded, (object)countProLoaded);
                this.subCateNumber.BeginInvoke(new Action(() => { subCateNumber.Text = sSubCateNumber; }));

                this.productNumber.BeginInvoke(new Action(() => { productNumber.Text = sProductNumber; }));

            }
            else
            {

                root.Nodes.Add(node);

                string last = rootText.Substring(rootText.Length - 1, 1);

                if (last.Equals(")"))
                {
                    string[] array = rootText.Split('(');

                    root.Text = array[0] + "(" + s + "/" + total + ")";

                }
                else
                    root.Text = root.Text + "(" + s + "/" + total + ")";
                //treeView1.EndUpdate();
            }




        }

        //add node to node farent
        private delegate void AddCallback3(TreeNode node, TreeNode node2, string rootText, string s, int total, int totalSubCate, int totalPro,
      int countSubCateLoaded, int countProLoaded);
        private void AddNode3(TreeNode node, TreeNode root, string rootText, string s, int total, int totalSubCate, int totalPro,
            int countSubCateLoaded, int countProLoaded)
        {
            //root = new TreeNode();
            string sSubCateNumber = countSubCateLoaded.ToString();

            string sProductNumber = countProLoaded.ToString();

            if (this.treeView1.InvokeRequired)
            {
                this.treeView1.BeginInvoke(new AddCallback3(AddNode3), (object)node, (object)root, (object)rootText,
                    (object)s, (object)total, (object)totalSubCate
                    , (object)totalPro, (object)countSubCateLoaded, (object)countProLoaded);
                this.subCateNumber.BeginInvoke(new Action(() => { subCateNumber.Text = sSubCateNumber; }));

                this.productNumber.BeginInvoke(new Action(() => { productNumber.Text = sProductNumber; }));

            }
            else
            {

                root.Nodes.Add(node);

                //string last = rootText.Substring(rootText.Length - 1, 1);

                //if (last.Equals(")"))
                //{
                //    string[] array = rootText.Split('(');

                //    root.Text = array[0] + "(" + s + "/" + total + ")";

                //}
                //else
                //    root.Text = root.Text + "(" + s + "/" + total + ")";
                //treeView1.EndUpdate();
            }




        }
        private void LoadProduct(Category category, TreeNode treeParent, int totalSubCategory, int totalProduct)
        {


            //_pauseEvent.WaitOne(Timeout.Infinite);

            //if (_shutdownEvent.WaitOne(0))
            //    break;

            // Stops the thread
            //if (stopEvent.WaitOne(0))
            //    return;

            //// Pauses the thread
            //pauseEvent.WaitOne(Timeout.Infinite);

            // Your code

            // Do the work..

            int j = 1;

            foreach (var subCate in category.SubCategorys)
            {

                TreeNode treeChild = new TreeNode();
                // treeChild.Text = (subCate.Name);
                treeChild.Text = Translate(subCate.Name);
                //subCateNumber.Text = j.ToString() + "/" + category.SubCategorys.Count;
                //this.AddNode(treeChild);
                if (flag == false)
                {
                countSubCateLoaded++;
                this.AddNode2(treeChild, treeParent, treeParent.Text, j.ToString(), category.SubCategorys.Count()
                    , totalSubCategory, totalProduct, countSubCateLoaded, countProLoaded);
                }
                else
                {
                    countProLoaded = 0;
                    countSubCateLoaded = 0;
                    break;
                }
                //this.AddNode2(treeChild, treeParent, treeParent.Text, j.ToString(), subCate.Count);
                ///////////// product//////////////////
                var productDoc = new HtmlDocument();
                productDoc.LoadHtml(GetGeneratedHTMLThread(subCate.Link + "?style=story"));
                var listHtmlProduct =
                    productDoc.DocumentNode.SelectNodes("//table[@class='DayView']//tbody//tr[@valign='top']");

                if (listHtmlProduct != null)
                {

                    int k = 1;
                    foreach (var productNode in listHtmlProduct)
                    {

                        HtmlDocument productDetailsDoc = new HtmlDocument();
                        productDetailsDoc.LoadHtml(productNode.InnerHtml);
                        //productNumber.Text = k.ToString() + "/" + listHtmlProduct.Count;

                        Product objProduct = new Product();
                        objProduct.Name = productDetailsDoc.DocumentNode.SelectSingleNode("//td[@class='PicDesc']//h3").InnerHtml;
                        objProduct.Link = productDetailsDoc.DocumentNode.SelectSingleNode("//td[@class='DayPic']//a//img").Attributes["src"].Value;
                        subCate.Products.Add(objProduct);
                        TreeNode nodeProduct = new TreeNode();
                        nodeProduct.Text = objProduct.Link;
                        //treeChild.Nodes.Add(nodeProduct);
                        //add node to node
                        //MessageBox.Show("" + k);
                        //if (flagControl == false)
                        //{
                        //totalProduct = listHtmlProduct.Count();
                        //if (flag == false)
                        //{
                        this.AddNode3(nodeProduct, treeChild, treeChild.Text, k.ToString(), listHtmlProduct.Count,
                            totalSubCategory, totalProduct, countSubCateLoaded, countProLoaded);
                        countProLoaded++;

                        //change name of treeChild
                        //Thread.Sleep(1);
                        //}
                        //else
                        //{
                        //    countProLoaded = 0;
                        //    countSubCateLoaded = 0;
                        //    break;
                        //}

                        //else
                        //{
                        //    break;
                        //}






                        k++;

                    }

                }
                j++;



            }









        }
        private void LoadParentNode(List<Category> listCategory)
        {
            foreach (var category in listCategory)
            {
                TreeNode treeParent = new TreeNode();
                treeParent.Text = category.Name;
                this.AddNode(treeParent);
            }
        }
        int totalProduct = 0;
        Thread thread;
        int totalCategoryLoaded = 0;
        public TreeView BuildTree(List<Category> listCategory, string s)
        {
            //TreeView treeView = new TreeView();
            // treeView.Nodes.Count();
            //if (flag == false)
            //{
                int totalSubCategory = CountTotalSubCategory(s);

                foreach (var category in listCategory)
                {
                    TreeNode treeParent = new TreeNode();
                    treeParent.Text = Translate(category.Name);
                    //   treeParent.Text = (category.Name);
                    totalCategoryLoaded++;
                    if (flag == false)
                    {
                        this.AddNode(treeParent);
                        this.cateNumber.BeginInvoke(new Action(() => { cateNumber.Text = totalCategoryLoaded + " "; }));
                    }
                    else
                    {
                        totalCategoryLoaded = 0;
                        countProLoaded = 0;
                        countSubCateLoaded = 0;
                        break;
                    }

                    ThreadStart starter = () => LoadProduct(category, treeParent, totalSubCategory, totalProduct);
                    thread = new Thread(starter);
                    thread.Start();

                //}
            }




            return treeView1;
        }
        //public TreeView AddNodeToNode(List<Category> listCategory)
        //{
        //    int totalSubCategory = CountTotalSubCategory();
        //    for (int j = 0; j < treeView1.Nodes.Count; j++)
        //    {
        //        for (int i = 0; i < listCategory.Count(); i++)
        //        {

        //            if (i == j)
        //            {

        //                ThreadStart starter = () => LoadProduct(listCategory[i], treeView1.Nodes[j], totalSubCategory, totalProduct);
        //                Thread thread = new Thread(starter);
        //                thread.Start();
        //                //LoadProduct(listCategory[i], treeView1.Nodes[i], totalSubCategory, totalProduct);
        //                break;
        //            }

        //        }
        //    }
        //    return treeView1;
        //}
        string URL = "";
        string GeneratedSource = "";

        public string GetGeneratedHTMLThread(string url)
        {
            URL = url;
            //WebBrowserThread();

            Thread t = new Thread(new ThreadStart(WebBrowserThread));
            Thread.Sleep(2000);
            t.SetApartmentState(ApartmentState.STA);
            // chỗ này khai báo trực tiếp luôn, ko dùng thread, gọi thread luôn nguyên ở trên
            t.Start();
            t.Join();

            return GeneratedSource;
        }
        private void WebBrowserThread()
        {
            WebBrowser wb = new WebBrowser();
            wb.Navigate(URL);
            wb.DocumentCompleted +=
            new WebBrowserDocumentCompletedEventHandler(
            wb_DocumentCompleted);

            //bool ready = false;
            //while (!ready)
            //{

            //        ready = wb.ReadyState != WebBrowserReadyState.Complete;

            //    Thread.Sleep(1000);
            //    // if you don't have any UI on this thread, DoEvent is redundant
            //    Application.DoEvents();
            //}


            while (wb.ReadyState != WebBrowserReadyState.Complete)
            {
                Thread.Sleep(2000);
                Application.DoEvents();

            }
            //Added this line, because the final HTML takes a while to show up
            GeneratedSource = wb.Document.Body.InnerHtml;
            wb.Dispose();
        }
        private void wb_DocumentCompleted(object sender,
        WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser wb = (WebBrowser)sender;
            GeneratedSource = wb.Document.Body.InnerHtml;
        }

        public string GetHmlSourceWeb(string linkWeb)
        {
            if (!string.IsNullOrEmpty(linkWeb))
            {
            AA:
                webBrowser1.Navigate(linkWeb);
                DateTime process = DateTime.Now;
                while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();
                    if (DateTime.Now.Subtract(process).Minutes > 2)
                    {
                        goto AA;
                    }
                }
            }
            return webBrowser1.DocumentText;
        }

        public void PopulateChildNode()
        {

        }
        private Infors GetDataInfor(string urlDetail)
        {
            if (string.IsNullOrEmpty(urlDetail))
            {
                return null;
            }
            var web = new HtmlWeb();

            HtmlDocument doc = web.Load(urlDetail);
            Infors info = new Infors();
            var name = doc.DocumentNode.SelectSingleNode("//h1[contains(@id,'primary-h1')]");
            if (name != null)
            {
                info.Name = name.InnerText;
            }
            var number =
                doc.DocumentNode.SelectSingleNode(
                    "//div[contains(@class,'main-content-wrapper')]//span[contains(@class,'contact-phone-number-obscured')]");
            if (number != null)
            {
                info.Number = number.GetAttributeValue("reveal-number", string.Empty);
            }
            return info;

        }
        private void Save()
        {
            var workbook = new HSSFWorkbook();
            var sheet = workbook.CreateSheet("Gumtree");
            var headerRow = sheet.CreateRow(0);
            headerRow.CreateCell(0).SetCellValue("Product Name");
            headerRow.CreateCell(1).SetCellValue("Phone No");
            headerRow.CreateCell(2).SetCellValue("Original Url");
            NPOI.SS.Util.CellRangeAddress cra = new NPOI.SS.Util.CellRangeAddress(0, 0, 4, 6);
            sheet.AddMergedRegion(cra);
            lock (m_locker)
            {
                for (int i = 0; i < m_infos.Count; i++)
                {
                    var company = m_infos[i];
                    var row = sheet.CreateRow(i + 1);
                    row.CreateCell(0).SetCellValue(company.Name);
                    row.CreateCell(1).SetCellValue(company.Number);
                    row.CreateCell(2).SetCellValue(company.OrginalUrl);
                }
                m_infos.Clear();
            }
            try
            {
                string fileName = Path.Combine(textBox2.Text, string.Format("gumtree.xls"));

                using (var file = new FileStream(fileName, FileMode.Create))
                {
                    workbook.Write(file);
                    file.Flush();
                    file.Close();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error!");
            }
        }
        private string Translate(string input)
        {
            //string input = "i eat rice";
            string languagePair = "zh-CN|en";

            string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
            WebClient webClient = new WebClient();
            webClient.Encoding = System.Text.Encoding.UTF8;
            string result = webClient.DownloadString(url);
            result = result.Substring(result.IndexOf("<span title=\"") + "<span title=\"".Length);
            result = result.Substring(result.IndexOf(">") + 1);
            result = result.Substring(0, result.IndexOf("</span>"));
            result = WebUtility.HtmlDecode(result.Trim());
            //MessageBox.Show(result);
            return result;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            var dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = dialog.SelectedPath;
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            //if (backgroundWorker1.IsBusy)
            //{

            //    backgroundWorker1.CancelAsync();
            //}
            //else
            //{
            //    backgroundWorker1.RunWorkerAsync();
            //}

            //this.backgroundWorker1.CancelAsync();

            // Disable the Cancel button.


            //flag = true;
            treeView1.Nodes.Clear();
            //ThreadMethod(true);
            totalSubCategory = 0;
           
            //treeView1 = null;
            countProLoaded = 0;
            countSubCateLoaded = 0;
            subCateNumber.Text = null;
            productNumber.Text = null;
            cateNumber.Text = null;
            if (backgroundWorker1.IsBusy == true &&
    backgroundWorker1.WorkerSupportsCancellation == true)
            {
                backgroundWorker1.CancelAsync();

            }
            if (backgroundWorker2.IsBusy == true &&
backgroundWorker2.WorkerSupportsCancellation == true)
            {
                backgroundWorker2.CancelAsync();

            }
            btnPause.Enabled = false;
            btnStop.Enabled = false;
            this.button1.Enabled = true;

        }
        public void ThreadMethod(Boolean sleepSwitch)
        {
            Console.WriteLine("newThread is executing ThreadMethod.");
            while (!sleepSwitch)
            {
                // Use SpinWait instead of Sleep to demonstrate the  
                // effect of calling Interrupt on a running thread.
                Thread.SpinWait(10000000);
            }
            try
            {
                Console.WriteLine("newThread going to sleep.");

                // When newThread goes to sleep, it is immediately  
                // woken up by a ThreadInterruptedException.
                Thread.Sleep(Timeout.Infinite);
            }
            catch (ThreadInterruptedException e)
            {
                Console.WriteLine("newThread cannot go to sleep - " +
                    "interrupted by main thread.");
            }
        }
        private void btnPause_Click(object sender, EventArgs e)
        {
            //isPause = true;
            //thread.Suspend();

            //btnPause.Enabled = false;
            //btnStop.Enabled = false;
            //this.button1.Enabled = true;
        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void yupoo_Load(object sender, EventArgs e)
        {
            flag = false;
        }







    }


    public class InforsTest
    {
        public string Name { get; set; }
        public string Number { get; set; }
        public string OrginalUrl { get; set; }
    }

    public class Category
    {
        public string Name { get; set; }
        public string Link { get; set; }
        public IList<SubCategory> SubCategorys { get; set; }

        public Category()
        {
            SubCategorys = new List<SubCategory>();
        }

    }

    public class SubCategory
    {
        public string Name { get; set; }
        public string Link { get; set; }
        public IList<Product> Products { get; set; }
        public SubCategory()
        {
            Products = new List<Product>();
        }
    }
    public class Product
    {
        public string Name { get; set; }
        public string Link { get; set; }
    }
}
